package com.dingwen.treasure.business.factory;

import cn.hutool.core.map.MapUtil;
import com.dingwen.treasure.business.service.ActivityStatusService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 　ActivityStatusFactory
 * 　@author dingwen
 * 　@date 2022/5/15$
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class ActivityStatusFactory implements ApplicationContextAware {

    /**
     * 应用程序上下文
     */
    private ApplicationContext applicationContext;


    /**
     * ActivityStatusService 工厂
     */
    public static Map<String, ActivityStatusService> ACTIVITY_STATUS_SERVICE = MapUtil.newHashMap(6);

    /**
     * 将所有活动变更服务状态到 bean 工厂
     *
     * @param applicationContext 应用程序上下文
     * @throws BeansException 异常
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        Map<String, ActivityStatusService> activityStatusServices = applicationContext.getBeansOfType(ActivityStatusService.class);
        activityStatusServices.forEach((type, service) -> ACTIVITY_STATUS_SERVICE.put(service.getStatusType(), service));
    }
}
